package mju.cxf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mju.cxf.constant.MessageConstant;
import mju.cxf.dto.CategoriesDTO;
import mju.cxf.entity.Activity;
import mju.cxf.entity.Categories;
import mju.cxf.entity.ProductSpecifications;
import mju.cxf.exception.BaseException;
import mju.cxf.mapper.CategoriesMapper;
import mju.cxf.mapper.ProductCategoriesMapper;
import mju.cxf.service.CategoriesService;
import mju.cxf.utils.TimestampUpdaterUtils;
import mju.cxf.vo.CategoriesVO;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author 郑献
 * @since 2024-10-09
 */

@Service
public class CategoriesServiceImpl extends ServiceImpl<CategoriesMapper, Categories> implements CategoriesService {
    @Autowired
    private PhotoServiceImpl photoServiceImpl;

    @Autowired
    private ProductCategoriesMapper productCategoriesMapper;

    /**
     * A分页查询分类信息
     * @param name
     * @return
     */
    @Override
    public Page<CategoriesVO> selectPage(String name, Integer type, Integer state, int current, int size) {
        Page<CategoriesVO> page = new Page<>(current, size);
        return this.baseMapper.selectPageVo(page, name, type, state);
    }



    /**
     * A新增分类信息
     * @param categoriesDTO
     * @return
     */
    @Override
    public boolean saveCategories(CategoriesDTO categoriesDTO) {
        try {
            Categories categories = new Categories();
            categoriesDTO.setState(1);
            categoriesDTO.setType(0);
            BeanUtils.copyProperties(categoriesDTO, categories);
            TimestampUpdaterUtils.saveUtil(categories);
            if (this.save(categories)) {
                if (categoriesDTO.getImageUrl() != null) {
                    photoServiceImpl.saveImages(categories.getImageUrl());
                }
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            throw new BaseException(MessageConstant.DATA_DUPLICATION);
        }
    }

    /**
     * A删除分类信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteCategories(Integer id) {
        Categories categories = this.getById(id);
        productCategoriesMapper.removeByCategoriesId(id);
        if (this.removeById(id)) {
            //删除图片路径
            photoServiceImpl.deleteUrl(categories.getImageUrl());
            return true;
        }
        return false;
    }

    /**
     * A修改分类信息
     * @param categoriesDTO
     * @return
     */
    @Override
    public boolean updateCategories(CategoriesDTO categoriesDTO) {
        Categories categories = new Categories();
        BeanUtils.copyProperties(categoriesDTO, categories);
        TimestampUpdaterUtils.updateUtil(categories);
        LambdaQueryWrapper<Categories> lqw = new LambdaQueryWrapper<>();
        Categories oneCategories = this.getOne(lqw.eq(Categories::getId, categoriesDTO.getId()));

        LambdaUpdateWrapper<Categories> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper
                .eq(Categories::getId, categories.getId())
                .set(Categories::getImageUrl, categories.getImageUrl());

        // 更新分类信息
        if (this.update(categories, updateWrapper)) {
            if (categoriesDTO.getImageUrl() != null && (!(Objects.equals(oneCategories.getImageUrl(), categoriesDTO.getImageUrl())))) {
                photoServiceImpl.deleteUrl(oneCategories.getImageUrl());
                photoServiceImpl.saveImages(categories.getImageUrl());
            }
            return true;
        }
        return false;
    }

    /**
     * A批量删除分类信息
     * @param ids
     * @return
     */
    @Override
    public boolean deleteCategorieses(List<Integer> ids) {
        for (Categories listById : this.listByIds(ids)) {
            if (this.removeById(listById.getId())) {}
            photoServiceImpl.deleteUrl(listById.getImageUrl());
        }
        return true;
    }

    /**
     * U获得主页商品列表
     *
     * @return
     */
    @Override
    public List<Categories> selectToChannel() {
        LambdaQueryWrapper<Categories> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Categories::getType,1);
        lqw.eq(Categories::getState, 0);
        return baseMapper.selectList(lqw);
    }
}
